package com.jy.rhin.domain.notification.participant.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * @author WeiQ
 * @date 2022/6/28 9:15
 * 参与者对象，包括通知发布者和主题订阅者
 */
@Data
@NoArgsConstructor
public class ParticipantDO {

    public boolean checkCodeAndRole(String participantCode, EnumParticipantRole participantRole) {
        //识别码唯一 不可修改
        if(!StringUtils.equals(participantCode, this.participantCode)){
            return false;
        }
        return participantRole == this.getParticipantRole();
    }

    public void maintainOriginalCreator(String createBy, Date createTime) {
        this.createBy = createBy;
        this.createTime = createTime;
    }

    /**
     * 创建一个参与者对象
     * @param id
     * @param createBy
     */
    public ParticipantDO(Long id, String createBy){
        this.id = id;
        this.createBy = createBy;
        this.createTime = new Date();
    }

    /**
     * 创建发布者对象
     * @param participantName
     * @param participantCode
     * @param participantUrl
     * @param participantDesc
     * @return
     */
    public ParticipantDO publisher(String participantName, String participantCode, String participantUrl, String participantDesc, String updateBy){
        this.participantName = participantName;
        this.participantCode = participantCode;
        this.participantUrl = participantUrl;
        this.participantDesc = participantDesc;
        this.interactionType = EnumInteractionType.HTTP;
        this.participantRole = EnumParticipantRole.PUBLISHER;
        this.updateBy = updateBy;
        this.updateTime = new Date();
        return this;
    }

    /**
     * 创建订阅者对象
     * @param participantName
     * @param participantCode
     * @param participantUrl
     * @param participantDesc
     * @return
     */
    public ParticipantDO subscriber(String participantName, String participantCode, String interactionType, String participantUrl, String participantDesc, String updateBy){
        this.participantName = participantName;
        this.participantCode = participantCode;
        this.participantUrl = participantUrl;
        this.participantDesc = participantDesc;
        this.participantRole = EnumParticipantRole.SUBSCRIBER;
        if(StringUtils.isEmpty(interactionType)){
            this.interactionType = EnumInteractionType.SOAP;
        }else{
            this.interactionType = EnumInteractionType.valueOf(interactionType);
        }
        this.updateBy = updateBy;
        this.updateTime = new Date();
        return this;
    }

    private Long id;
    /**
     * 参与者名称
     */
    private String participantName;
    /**
     * 参与者代码;全局唯一
     */
    private String participantCode;
    /**
     * 参与者角色;PUBLISHER:发布者, SUBSCRIBER:订阅者
     */
    private EnumParticipantRole participantRole;
    /**
     * 互动方式 SOAP HTTP
     */
    private EnumInteractionType interactionType;
    /**
     * 参与者端口url
     */
    private String participantUrl;
    /**
     * 描述
     */
    private String participantDesc;
    /**
     * 创建人
     */
    private String createBy;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 更新人
     */
    private String updateBy;
    /**
     * 更新时间
     */
    private Date updateTime;



}
